﻿using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RabbitMQ.Subscriber
{
    class Program
    {
        static void Main(string[] args)
        {
            var factory = new ConnectionFactory() { HostName = "localhost" };
            using (var connection = factory.CreateConnection())
            {
                using (var chanel = connection.CreateModel())
                {
                    chanel.ExchangeDeclare(exchange: "logs", type: "fanout");
                    var queueName = chanel.QueueDeclare().QueueName;
                    chanel.QueueBind(queue: queueName, exchange: "logs", routingKey: "", arguments: null);

                    var consumer = new EventingBasicConsumer(chanel);
                    consumer.Received += Consumer_Received;
                    chanel.BasicConsume(queue: queueName, autoAck: true, consumer: consumer);
                    Console.WriteLine("Press [enter] to exit.");
                    Console.ReadKey();
                }

            }
        }

        private static void Consumer_Received(object sender, BasicDeliverEventArgs e)
        {
            var message = Encoding.UTF8.GetString(e.Body);
            Console.WriteLine("[x] {0}", message);
        }
    }
}
